package g4p.newg4p;

import g4p_controls.G4P;
import g4p_controls.GAlign;
import g4p_controls.GButton;
import g4p_controls.GCheckbox;
import g4p_controls.GClip;
import g4p_controls.GEvent;
import g4p_controls.GKnob;
import g4p_controls.GLabel;
import g4p_controls.GOption;
import g4p_controls.GSlider;
import g4p_controls.GToggleControl;
import g4p_controls.GToggleGroup;
import g4p_controls.GValueControl;
import processing.core.PApplet;

/**
 * This program demonstrates the various configuration options available
 * for the knob control (GKnob).
 * 
 * The only thing not set is the range limits. By default the knob returns
 * vales in the range 0.0 to 1.0 inclusive. Use setLimits() to set your 
 * own range.
 * 
 * @author Peter Lager
 *
 */
public class Knob_Test extends PApplet {
	GKnob kb;
	int bgcol = 128;
	
	public void setup(){
		size(500,360);
		G4P.setCursor(CROSS);
		kb = new GKnob(this, 60,60,180,180,0.8f);
		makeKnobConfigControls();
	}

	public void draw(){
		background(bgcol);
		fill(227,230,255);
		noStroke();
		rect(width - 190, 0, 200, height);
		rect(0, height - 84 , width - 190, 84);
	}

	public void handleKnobEvents(GValueControl knob, GEvent event) { 
		// Has the demo knob turned?
		if(kb == knob)
			lblValue.setText("" + knob.getValueS());
		// These 2 knobs are part to configure the demo
		else if(knbStart == knob || knbEnd == knob)
			this.kb.setTurnRange(knbStart.getValueF(), knbEnd.getValueF());
	}

	public void handleButtonEvents(GButton button, GEvent event) { 
		if(button.tagNo >= 1000){
			kb.setLocalColorScheme(button.tagNo - 1000);
			lblValue.setLocalColorScheme(button.tagNo - 1000);
		}
		else if(btnMakeCode == button)
			placeCodeOnClipboard();
	}
	
	public void handleSliderEvents(GValueControl slider, GEvent event) { 
		if(slider == sdrSense)
			kb.setSensitivity(slider.getValueF());
		else if(slider == sdrSense)
			kb.setSensitivity(slider.getValueF());		
		else if(slider == sdrEasing)
			kb.setEasing(slider.getValueF());		
		else if(slider == sdrNbrTicks)
			kb.setNbrTicks(slider.getValueI());		
		else if(slider == sdrBack)
			bgcol = slider.getValueI();	
		else if (slider == sdrGripRatio)
			kb.setGripAmount(slider.getValueF());
	}

	public void handleToggleControlEvents(GToggleControl option, GEvent event) {
		if(option == optAngular)
			kb.setTurnMode(G4P.CTRL_ANGULAR);
		else if(option == optXdrag)
			kb.setTurnMode(G4P.CTRL_HORIZONTAL);
		else if(option == optYdrag)
			kb.setTurnMode(G4P.CTRL_VERTICAL);
		else if(option == cbxOverArc)
			kb.setOverArcOnly(option.isSelected());
		else if(option == cbxOverAll)
			kb.setIncludeOverBezel(option.isSelected());
		else if(option == cbxOpaque)
			kb.setOpaque(option.isSelected());
		else if(option == cbxShowArcOnly)
			kb.setShowArcOnly(option.isSelected());
		else if(option == cbxShowTicks)
			kb.setShowTicks(option.isSelected());
		else if(option == cbxShowTrack)
			kb.setShowTrack(option.isSelected());
		else if(option == cbxSticky)
			kb.setStickToTicks(option.isSelected());
	}


	private void placeCodeOnClipboard() {
		StringBuilder s = new StringBuilder();
		s.append("// Generated by the GKnob example program\n\n");
		s.append("import guicomponents.*;\n\n");
		s.append("GKnob kb; \n\n");
		s.append("void setup() { \n");
		s.append("  size(300, 300); \n");
		s.append("  kb = new GKnob(this, 60, 60, 180, 180, " + sdrGripRatio.getValueF() + "); \n");
		s.append("  // Some of the following statements are not actually\n");
		s.append("  // required because they are setting the default value. \n");
		s.append("  kb.setTurnRange("+knbStart.getValueF()+", " + knbEnd.getValueF() + "); \n");
		s.append("  kb.setLocalColorScheme(" + kb.getLocalColorScheme() + "); \n");
		s.append("  kb.setOpaque(" + kb.isOpaque() + "); \n");
		s.append("  kb.setValue(" + kb.getValueF() + "); \n");
		s.append("  kb.setNbrTicks(" + kb.getNbrTicks() + "); \n");
		s.append("  kb.setShowTicks(" + kb.isShowTicks() + "); \n");
		s.append("  kb.setShowTrack(" + kb.isShowTrack() + "); \n");
		s.append("  kb.setShowArcOnly(" + kb.isShowArcOnly() +"); \n");
		s.append("  kb.setStickToTicks(" + kb.isStickToTicks() + "); \n");
		s.append("  kb.setTurnMode(" + kb.getTurnMode() + "); \n");
		s.append("  kb.setIncludeOverBezel(" + kb.isIncludeOverBezel() + "); \n");
		s.append("  kb.setOverArcOnly(" + kb.isOverArcOnly() + "); \n");
		s.append("  kb.setSensitivity(" + kb.getSensitivity() + "); \n");
		s.append("  kb.setEasing(" + kb.getEasing() + "); \n");
		s.append("}   \n\n");
		s.append("void draw(){ \n");
		s.append("  background(" + bgcol + "); \n");
		s.append("} \n");
		if(GClip.copy(s.toString()))
			println("Paste code into empty Processing sketch");
		else
			System.err.println("UNABLE TO ACCESS CLIPBOARD");
	}

	// =================================================================================
	// ===================            New pde Tab             ==========================

	GToggleGroup tg = new GToggleGroup();
	GOption optAngular, optXdrag, optYdrag;
	GCheckbox cbxOpaque, cbxOverArc, cbxOverAll;
	GCheckbox cbxShowTicks, cbxShowArcOnly, cbxShowTrack;
	GCheckbox cbxSticky;
	GSlider sdrBack, sdrSense, sdrNbrTicks, sdrEasing, sdrGripRatio;
	GLabel lblMC, lblVisible, lblSense, lblNbrTicks;
	GLabel lblEasing, lblValue,lblAngles, lblGripAmount;
	GKnob knbStart, knbEnd;
	GButton btnMakeCode;
	GButton[] btnColours = new GButton[8];

	public void makeKnobConfigControls(){
		// Create colour scheme selectors
		int x = width - 42, y = 2;
		for(int i = 0; i < btnColours.length; i++){
			btnColours[i] = new GButton(this, x, y + i * 20, 40, 18, "" + (i+1));
			btnColours[i].tag = "Button: " + (i+1);
			btnColours[i].setLocalColorScheme(i);
			btnColours[i].tagNo = 1000+i;
		}	
		// Create sliders
		x = width-100; y = 162;
		sdrBack = new GSlider(this, x, y, 162, 80, 12);
		sdrBack.setLimits(bgcol, 0, 255);
		sdrBack.setRotation(-PI/2);
		sdrBack.setTextOrientation(G4P.ORIENT_RIGHT);
		sdrBack.setEasing(20);
		sdrBack.setShowValue(true);
		sdrBack.setShowTicks(true);
		
		x = width - 180; y = 250;
		sdrSense = new GSlider(this, x, y, 80, 40, 12);
		sdrSense.setLimits(1.0f, 0.2f, 5.0f);
		sdrSense.setShowValue(true);
		lblSense = new GLabel(this, x + 82, y, 100, 40, "XY drag rate");
		lblSense.setTextAlign(GAlign.LEFT, null);
		lblSense.setTextItalic();
		
		sdrEasing = new GSlider(this, x, y + 34, 80, 40, 12);
		sdrEasing.setLimits(1.0f, 1.0f, 30.0f);
		sdrEasing.setShowValue(true);
		lblEasing = new GLabel(this, x + 82, y + 34, 80, 40, "Easing");
		lblEasing.setTextAlign(GAlign.LEFT, null);
		lblEasing.setTextItalic();
		
		sdrNbrTicks = new GSlider(this, x, y + 68, 80, 40, 12);
		sdrNbrTicks.setLimits(2, 2, 15);
		sdrNbrTicks.setShowValue(true);
		lblNbrTicks = new GLabel(this, x + 82, y + 68, 80, 40, "No. of ticks");
		lblNbrTicks.setTextAlign(GAlign.LEFT, null);
		lblNbrTicks.setTextItalic();
		
		x = width - 180; y = 164;
		lblMC = new GLabel(this, x, y, 178, 18, "Knob turning control");
		lblMC.setTextAlign(GAlign.LEFT, null);
		lblMC.setTextBold();
		optAngular = new GOption(this, x, y + 20, 80, 18, "Angular");
		optXdrag = new GOption(this, x, y + 40, 80, 18, "X drag");
		optYdrag = new GOption(this, x, y + 60, 80, 18, "Y drag");
		tg.addControls(optAngular, optXdrag, optYdrag);
		optXdrag.setSelected(true);
		cbxOverArc = new GCheckbox(this, x + 90, y + 20, 70, 18, "Arc only");
		cbxOverAll = new GCheckbox(this, x + 90, y + 40, 90, 18, "Incl. Bezel");

		x = width - 180; y = 2;
		lblVisible = new GLabel(this, x, y, 70, 18, "Visible");
		lblVisible.setTextBold();
		cbxShowTicks = new GCheckbox(this, x, y + 20, 70, 18, "Ticks");
		cbxShowTicks.setSelected(true);
		cbxShowTrack = new GCheckbox(this, x, y + 40, 70, 18, "Track");
		cbxShowTrack.setSelected(true);
		cbxShowArcOnly = new GCheckbox(this, x, y + 60, 70, 18, "Arc Only");
		cbxOpaque = new GCheckbox(this, x, y + 80 , 70, 18, "Opaque");
		cbxSticky = new GCheckbox(this, x, y + 110, 70, 40, "Stick to ticks");
		
		x = (int) (kb.getCX() - 40);
		y = (int) (kb.getY() -40);
		lblValue = new GLabel(this, x, y, 80, 30);
		lblValue.setOpaque(true);
		lblValue.setText("" + kb.getValueS());

		x = 5; y = height - 80;
		knbStart = new GKnob(this, x, y, 50, 50, 1);
		knbStart.setTurnRange(0, 360);
		knbStart.setLimits(110, 0, 360);
		knbStart.setTurnMode(G4P.CTRL_ANGULAR);
		knbEnd = new GKnob(this, x + 50, y, 50, 50, 1);
		knbEnd.setTurnRange(0, 360);
		knbEnd.setLimits(70, 0, 360);
		knbEnd.setTurnMode(G4P.CTRL_ANGULAR);
		lblAngles = new GLabel(this, 0, y + 55, 110, 20, "Turn Limits");
		sdrGripRatio = new GSlider(this, x + 110, y, 120, 40, 12);
		sdrGripRatio.setShowDecor(false, false, true, false);
		sdrGripRatio.setValue(0.8f);
		lblGripAmount = new GLabel(this, x + 226, y, 80, 40, "Grip Ratio");
		btnMakeCode = new GButton(this, x + 110, y + 36, 182, 38, "Place code for existing configuration on clipboard");
	}


}
